package com.mjh.action;

import java.util.Date;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.mjh.bean.Discuss;
import com.mjh.comm.Constant;
import com.mjh.comm.page.Pager;
import com.mjh.manager.DiscussManager;

public class DiscussAction extends ActionSupport{

	private static final long serialVersionUID = -5969814568349114819L;
	
	public static Logger log = Logger.getLogger(DiscussAction.class);
	
	private DiscussManager manager = new DiscussManager();
	
	private Discuss discuss;
	
	private Pager<Discuss> pager;
	
	private HashMap<String,String> rp;
	
	private int result;
	
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getRemoteAddr();
		this.discuss.setIp(ip);
		this.discuss.setPostTime(new Date());
		this.result = manager.addDiscuss(this.discuss);
		this.result = manager.updateDiscussId(this.discuss.getId());
		return SUCCESS;
	}
	
	public String reply(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getRemoteAddr();
		this.discuss.setIp(ip);
		this.discuss.setPostTime(new Date());
		this.discuss.setIsReply(Constant.IS_REPLY);
		this.result = manager.addDiscuss(this.discuss);
		return SUCCESS;
	}
	
	public String del(){
		result = manager.delDiscuss((NumberUtils.toInt(rp.get("discussId"))));
		return SUCCESS;
	}

	public String list(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("articleId", rp.get("articleId"));
		this.pager = manager.listAll(NumberUtils.toInt(rp.get("pageNo")), parameter);
		return SUCCESS;
	}
	
	public String listForAdmin(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("orderByAdmin", true);
		this.pager = manager.listAll(NumberUtils.toInt(rp.get("pageNo")), parameter);
		return SUCCESS;
	}

	public Discuss getDiscuss() {
		return discuss;
	}

	public void setDiscuss(Discuss discuss) {
		this.discuss = discuss;
	}

	public HashMap<String, String> getRp() {
		return rp;
	}

	public void setRp(HashMap<String, String> rp) {
		this.rp = rp;
	}

	public Pager<Discuss> getPager() {
		return pager;
	}

	public void setPager(Pager<Discuss> pager) {
		this.pager = pager;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}
}
